<template>
	<view class="pageBox">
		<head-navbar :back="false">
			<view class="">购物车</view>
			<view slot="right" @click="editShow === true ? editShow = false :editShow = true;" v-if="editShow === false" style="color: #333333;">编辑</view>
			<view slot="right" @click="editShow === true ? editShow = false :editShow = true;" v-else style="color: #333333;">完成</view>
		</head-navbar>

			<view class="goodsList" v-if="goodsList.length > 0">
				<view v-for="(items,idx) in goodsList" :key="items.id" class="shopBox">
					<!-- <view style="padding: 20upx;" class="select-shop">
						<radio class="goodsSelecet" color="#fc3d42" value="r1" :checked="items.Check" @click.stop="toSelect('shop',idx)"
						 style="transform:scale(0.6)" />
						<text style="padding-left: 10upx;">{{items.merchantName}}{{items.goodsId}}</text>
					</view> -->
					<view class="item" v-for="(item,index) in items.list" @click="toPage(0,'/pages/goodsDetail/index?goodsId='+item.goodsId+'&goodsType=1')">
						<radio class="goodsSelecet" color="#FE5900" value="r1" :checked="item.checked" @click.stop="toSelect('goods',idx,index)" />
						<image :src="item.goodsImg"></image>
						<view class="right">
							<view class="Topbox">
								<view class="itemTop">
									<view class="title">{{item.goodsName}}</view>

								</view>
								<view class="tip">{{item.goodsSpecsName}}</view>
							</view>
							<view class="bottom">
								<view class="box">
									<view class="price">￥{{item.unitPrice}}</view>
								</view>
								<view class="box2"  @click.stop="toStop"><u-number-box v-model="item.quantity" :disabled-input="true" :min="1" @plus="Increment(item.shoppingCartId)" @minus="Decrement(item.shoppingCartId)"></u-number-box></view>
							</view>
						</view>
					</view>
				</view>

			</view>
			<view v-else class="bgTips">
				<image src="../../static/img/guangg.png" mode="" style="width: 266upx;height: 234upx;"></image>
				<view class="txt">空空如也</view>
				<button type="default" class="btn" @click="toPage(3,'/pages/index/index')">去逛逛</button>
			</view>
		<view style="height: 120upx;"></view>
		<view class="btns" v-if="goodsList.length != 0">
			<label class="radio">
				<radio color="#FE5900" value="r1" :checked="allSelect" @click="onAllSelect" style="transform: scale(0.6);" />全选</label>
			<view class="right" v-show="editShow === false">
				<view class="box">
					<view class="text">合计：</view>
					<view class="price"><span style="padding-left: 10upx;">￥{{allPriceCart}}</span></view>
				</view>
				<button size="mini" :loading="loading" :disabled="loading" @click="toCreated()" class="payBtn">去结算</button>
			</view>
			<view class="editRight" v-show="editShow === true">
				<image class="delete" src="../../static/img/delete.png" @click="toDelete()"></image>
			</view>
		</view>
	</view>
</template>

<script>
	import * as api from '@/api/goods/index.js';
	export default {
		data() {
			return {
				loading: false,
				allSelect: false,
				goodsList: [],
				allPriceCart: 0,
				allCoupons:0,
				topHeight: 0,
				btnsHeight: 0,
				value: 1,
				height: 0,
				editShow: false,
				choseList: [],
				ids:[],
				ids2:[]
			}
		},
		onReady() {},
		onPullDownRefresh() {
				this.ids=[]
				this.ids2=[]
				this.init();
		       setTimeout(function () {
		           uni.stopPullDownRefresh();
		       }, 1000);
		   },
		mounted() {},
		watch: {
			'goodsList': {
				handler: function(newValue, oldValue) {
					var allPrcie = 0;
					var coupons = 0
					var show = true;
					for (var k = 0; k < this.goodsList.length; k++) {
						var list = this.goodsList[k].list
						for (var i = 0; i < list.length; i++) {
							if (list[i].checked == true) {
								// if(list[i].goodsType == 1){
								// 	coupons = coupons + (list[i].quantity * list[i].coupons);
								// }else{
									allPrcie = allPrcie + (list[i].quantity * list[i].unitPrice);
								// }
							} else {
								show = false;
							}
						}
						this.goodsList[k].check = show;
					}
					this.goodsList.forEach(e => {
						e.Check == true ? this.allSelect = true : this.allSelect = false;
					})
					this.allPriceCart = allPrcie.toFixed(2)
					this.allCoupons = coupons.toFixed(2)
				},
				deep: true,
			}
		},
		computed: {},
		onLoad() {
			this.ids=[]
			this.ids2=[]
		},
		onShow() {
           this.init();
		},
		onUnload() {},
		methods: {
			toCreated() {
				let carList =[]
				let myGoodsList =[]
				for (var i = 0; i < this.goodsList.length; i++) {
						for (var j = 0; j < this.goodsList[i].list.length; j++) {
							if (this.goodsList[i].list[j].checked == true) {
								myGoodsList.push(this.goodsList[i].list[j])
								this.ids.push(this.goodsList[i].list[j].shoppingCartId)
								if(this.goodsList[i].list[j].goodsType != 1){
									this.ids2.push(this.goodsList[i].list[j].shoppingCartId)
								}
						      }
					     }
						 console.log(myGoodsList)
						 if(myGoodsList.length>0){
							 carList.push(myGoodsList)
						 }
						 myGoodsList =[]
				      }
					  console.log(carList)
				if(carList.length == 0){
					uni.showToast({
						title:'请选择商品',
						icon:'none'
					})
					return
				}
				uni.setStorageSync('cartList', carList)
				uni.navigateTo({
					url: '/pages/order/createOrder?orderType=2&ids='+this.ids +'&price=' + this.allPriceCart+'&allCoupons='+this.allCoupons + '&ids2=' + this.ids2
				})
			},
			init() {
				this.loading = false;
				this.allSelect = false;
				this.editShow = false;
				this.getCarList();
			},
			toStop(){
				console.log(123)
				return true
			},
			toSelect(type, index, idx) {
				switch (type) {
					case 'shop':
						if (this.goodsList[index].Check === false) {
							this.goodsList[index].Check = true;
							this.goodsList[index].list.forEach(e => {
								e.checked = true;
							});
						} else {
							this.goodsList[index].Check = false;
							console.log(this.goodsList[index])
							this.goodsList[index].list.forEach(e => {
								e.checked = false;
							})
						}
						break;
					case 'goods':
						this.goodsList[index].list[idx].checked == false ? this.goodsList[index].list[idx].checked = true : this.goodsList[
							index].list[idx].checked = false;
						break;
					default:
						break;
				}
			},
			onAllSelect() {
				this.allSelect === false ? this.allSelect = true : this.allSelect = false;
				if (this.allSelect) {
					this.goodsList.forEach(e => {
						e.Check = true;
						e.list.forEach(i => {
							i.checked = true;
						})
					})
				} else {
					this.goodsList.forEach(e => {
						e.Check = false;
						e.list.forEach(i => {
							i.checked = false;
						})
					})
				}
			},
			toDelete() {
				var goodList = this.goodsList;
				let deleteList = [];
				if (this.allSelect) {
					this.allDelte();
				} else {
					console.log(goodList)
					for (let i = 0; i < goodList.length; i++) {
						if (goodList[i].Check) {
							goodList.list.forEach(e => {
								deleteList.push(e.shoppingCartId);
							})
							console.log(deleteList)
						} else {
							for (var k = 0; k < goodList[i].list.length; k++) {
								if (goodList[i].list[k].checked == true) {
									deleteList.push(goodList[i].list[k].shoppingCartId);
									console.log(deleteList)
								}
							}
						}
					}
					this.DelteCart(deleteList);
				}
			},
			edit() {
				this.editShow === true ? this.editShow = false : this.editShow = true;
			},
			// 获取购物车列表
			async getCarList() {
				try {
					const ret = await api.getCarList();
					var arr = ret.data.obj;
					var list = [];
					var obj = {}
					for (var i = 0; i < arr.length; i++) {
						obj = {
							merchantName: '',
							Check: false,
							list: []
						}
						arr[i].forEach(e => {
							e.checked = false;
							obj.list.push(e);
							obj.merchantName = e.merchantName;
						})
						list.push(obj);
					}
					this.goodsList = list;
				} catch (error) {

				}
			},
			// 全选删除购物车
			async allDelte() {
				try {
					const ret = await api.deleteAllCarList();
					this.init();
				} catch (error) {

				}
			},
			// 删除购物车商品
			async DelteCart(list) {
				try {
					const ret = await api.deleteCartGoods({
						ids: list
					});
					this.init();
				} catch (error) {

				}
			},
			// 数量增加
			async Increment(list) {
				try {
					const ret = await api.CartGoodsIncrement({
						shoppingCartId: list
					});
				} catch (error) {

				}
			},
			// 数量减少
			async Decrement(list) {
				try {
					const ret = await api.CartGoodsDecrement({
						shoppingCartId: list
					});
				} catch (error) {

				}
			},
		}
	}
</script>

<style lang="scss">
	@import "./index.scss";

	.editRight {
		margin-right: 30upx;
	}
</style>
